package com.level2app.ui.welcome;

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Paint;
import android.text.SpannableString;

import androidx.core.app.ActivityCompat;

import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.level2app.R;
import com.level2app.base.BaseActivity;
import com.level2app.base.BaseViewModel;
import com.level2app.databinding.ActivityWelcomeScanQrActivityBinding;
import com.level2app.utils.JumpIntent;
import com.level2app.utils.TextChangeUtils;
import com.level2app.view.dialog.CommonDialog;

import java.util.List;

/**
 * 欢迎页2
 */
public class WelcomeScanQRActivity extends BaseActivity<ActivityWelcomeScanQrActivityBinding, BaseViewModel> {

    @Override
    protected BaseViewModel bindModel() {
        return getViewModel(this, BaseViewModel.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void initData() {
        initTitleBar();
        binding.enterCodeTv.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        binding.enterCodeTv.getPaint().setAntiAlias(true);
        String text = getString(R.string.scan_your_invitation_qr_code_hint);
        SpannableString spannableString = new SpannableString(text);
        TextChangeUtils.matcherSearchTextItalic(spannableString, text, "Invitation QR code");
        TextChangeUtils.matcherSearchTextItalic(spannableString, text, "Invitation code");
        TextChangeUtils.matcherSearchTextBold(spannableString, text, "Invitation QR code");
        TextChangeUtils.matcherSearchTextBold(spannableString, text, "Invitation code");
        binding.contentTv.setText(spannableString);
    }

    @Override
    protected void initClick() {
        super.initClick();
        viewModel.onDelayClick(binding.enterCodeTv, o -> {
            //输入code
            JumpIntent.jump(this, ReferralCodeActivity.class);
        });
        viewModel.onDelayClick(binding.scanQRBtn, o -> {
            //扫码
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                //弹窗
                CommonDialog commonDialog = new CommonDialog(this) {
                    @Override
                    public void leftClick() {

                    }

                    @Override
                    public void rightClick() {
                        super.rightClick();
                        XXPermissions.with(WelcomeScanQRActivity.this)
                                // 申请单个权限
                                .permission(Permission.CAMERA)
                                .request(new OnPermissionCallback() {

                                    @Override
                                    public void onGranted(List<String> permissions, boolean all) {
                                        if (all) {
                                            JumpIntent.jump(WelcomeScanQRActivity.this, ScanQRActivity.class);
                                        }
                                    }

                                    @Override
                                    public void onDenied(List<String> permissions, boolean never) {
                                        if (never) {
                                            // 如果是被永久拒绝就跳转到应用权限系统设置页面
                                            XXPermissions.startPermissionActivity(WelcomeScanQRActivity.this, permissions);
                                        }
                                    }
                                });
                    }
                };
                commonDialog.setText(getString(R.string.level_app_would_like_to), getString(R.string.enabling_camera_access_lets_you_take_photos));
                commonDialog.showDialog();
            } else {
                JumpIntent.jump(WelcomeScanQRActivity.this, ScanQRActivity.class);
            }
        });
    }
}